// pages/menu/menu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuId:0,
    currentIndex: 0,
    coffeeId: "coffee-0",
    scrollHeight: "",
    searhStyle: "",
    menuList: [],
    newPeopleCoffee:[]
  },
  //新人喝什么
  newPeople(){
    wx.cloud.callFunction({
      name:"getCoffeeList"
    }).then(res =>{
      this.setData({
        newPeopleCoffee:res.result.data
      })
      console.log(this.data.newPeopleCoffee)
    })
  },
  //跳转到商品详情页面
  shop(event){
    const id = this.data.menuList[event.currentTarget.dataset.idy].coffeeList[event.currentTarget.dataset.idx]._id
    wx.navigateTo({
      url: '/pages/shopItem/shopItem?id='+id,
    })
  },
   //跳转到商品详情页面
   shop2(event){
    const id = event.currentTarget.dataset.idx
    wx.navigateTo({
      url: '/pages/shopItem/shopItem?id='+id,
    })
  },
  //获取导航栏的位置
  getMenuScrollHight() {
    let _that = this
    wx.getSystemInfo({
      success(res) {
        let scrollHeight = res.windowHeight * 0.7 + "rpx";
        console.log(scrollHeight)
        _that.setData({
          scrollHeight: "height:" + scrollHeight
        })
      }
    })
  },
  //获取左侧导航菜单scroll的高度
  getLeftMenuScrollHeight() {
    let _that = this
    wx.getSystemInfo({
      success(res) {
        let scrollHeight = res.windowHeight * 0.72 + "px"
        _that.setData({
          leftScrollHeight: "height:" + scrollHeight
        })
      }
    })
  },
  //左侧菜单跳转
  changeMenu(event) {
    console.log(event.currentTarget.dataset)
    let {
      id
    } = event.currentTarget.dataset
    this.setData({
      coffeeId: "coffee-"+id,
      menuId:id
    })
  },
  getCenterPostion() {
    //代码写在这里 
    //获取胶囊的样式内容
    let { top, height } = wx.getMenuButtonBoundingClientRect()
    let centerPosition = top + height / 2
    let searhStyle = "margin-top:calc(" + centerPosition + "px - 26rpx)"
    this.setData({
      searhStyle: searhStyle
    })
    console.log(centerPosition)
  },
  getStyleAndCoffee(){
    wx.cloud.callFunction({
      name:"getCoffeeAndStyle"
    }).then(res => {
      console.log(res)
      this.setData({
        menuList:res.result.list
      })
      console.log(this.data.menuList)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMenuScrollHight()
    this.getCenterPostion()
    this.getLeftMenuScrollHeight()
    this.getStyleAndCoffee()
    this.newPeople()
  },
  //跳转搜索页面
  searchShop() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  //选项卡切换
  checkTab(event) {
    let { index } = event.currentTarget.dataset
    if (index == this.data.currentIndex) {
      return false
    } else {
      this.setData({
        currentIndex: index
      })
    }
  },
  changTab(event) {
    console.log(event)
    let index = event.detail.current
    this.setData({
      currentIndex: index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})